<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Controller for managing login credentials.
 * @package backend-authentication/controllers
 * @category Controller
 * 
 * @author Mark Angelo Angulo
 * @copyright (c) 2015
 */
class Logout extends Backend_Controller {
    
    /**
     *  Constructor method.
     */
    function __construct()
    {
        parent::__construct();
        
        $this->load->config('cms');
        $this->load->helper('authentication');
        
        // set cms configurations
        $cms                    = array();
        $cms['title']           = $this->config->item('title');
        $cms['db_prefix']       = $this->config->item('db_prefix');
        $this->cms = $cms;
        
        // retrieve more cms configuration from database
        $this->load->model('backend-config/cms_config_model');
        
        // set configuration from database
        $cms_configs = $this->cms_config_model->all()->get();
        foreach( $cms_configs as $cms_config )
        {
            $cms[ $cms_config['key'] ] = $cms_config['value'];
        }
        $this->cms = $cms;
        
        // load necessary models
        $this->load->model('backend-companies/company_model');
    }
    
    /**
     * Logs the user out.
     * Then redirect to the login page.
     * @access private
     */
    public function index()
    {
        $cms_config             = $this->cms;
        $default_company_id     = $this->company_model->attribute( array('name' => $cms_config['company']), 'id')->get();
        $company_id             = $this->session->userdata('company_id') ? $this->session->userdata('company_id') : $default_company_id;
        
        // log the user out
        $this->ion_auth->logout();
        $this->session->sess_destroy();

        // set message
        $message = array();
        $message['class']       = 'alert-info';
        $message['title']       = 'Logged Out';
        $message['message']     = 'You have been logged out.';
        $message['ion_message'] = $this->ion_auth->messages();

        goto_login($company_id, $message);
    }
}

/* End of file logout.php */
/* Location: ./application/modules/backend-authentication/controllers/logout.php */